package idv.loveloser.dao;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * MdlForumPosts entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see idv.loveloser.dao.MdlForumPosts
 * @author MyEclipse Persistence Tools
 */

public class MdlForumPostsDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory
			.getLogger(MdlForumPostsDAO.class);
	// property constants
	public static final String DISCUSSION = "discussion";
	public static final String PARENT = "parent";
	public static final String USERID = "userid";
	public static final String CREATED = "created";
	public static final String MODIFIED = "modified";
	public static final String MAILED = "mailed";
	public static final String SUBJECT = "subject";
	public static final String MESSAGE = "message";
	public static final String MESSAGEFORMAT = "messageformat";
	public static final String MESSAGETRUST = "messagetrust";
	public static final String ATTACHMENT = "attachment";
	public static final String TOTALSCORE = "totalscore";
	public static final String MAILNOW = "mailnow";

	public void save(MdlForumPosts transientInstance) {
		Transaction trx = getSession().beginTransaction();
		log.debug("saving MdlForumPosts instance");
		try {
			getSession().save(transientInstance);
			trx.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			trx=null;
			log.error("save failed", re);
			throw re;
		}
		trx=null;
	}

	public void delete(MdlForumPosts persistentInstance) {
		log.debug("deleting MdlForumPosts instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public MdlForumPosts findById(java.lang.Long id) {
		log.debug("getting MdlForumPosts instance with id: " + id);
		try {
			MdlForumPosts instance = (MdlForumPosts) getSession().get(
					"idv.loveloser.dao.MdlForumPosts", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(MdlForumPosts instance) {
		log.debug("finding MdlForumPosts instance by example");
		try {
			List results = getSession()
					.createCriteria("idv.loveloser.dao.MdlForumPosts")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding MdlForumPosts instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from MdlForumPosts as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByDiscussion(Object discussion) {
		return findByProperty(DISCUSSION, discussion);
	}

	public List findByParent(Object parent) {
		return findByProperty(PARENT, parent);
	}

	public List findByUserid(Object userid) {
		return findByProperty(USERID, userid);
	}

	public List findByCreated(Object created) {
		return findByProperty(CREATED, created);
	}

	public List findByModified(Object modified) {
		return findByProperty(MODIFIED, modified);
	}

	public List findByMailed(Object mailed) {
		return findByProperty(MAILED, mailed);
	}

	public List findBySubject(Object subject) {
		return findByProperty(SUBJECT, subject);
	}

	public List findByMessage(Object message) {
		return findByProperty(MESSAGE, message);
	}

	public List findByMessageformat(Object messageformat) {
		return findByProperty(MESSAGEFORMAT, messageformat);
	}

	public List findByMessagetrust(Object messagetrust) {
		return findByProperty(MESSAGETRUST, messagetrust);
	}

	public List findByAttachment(Object attachment) {
		return findByProperty(ATTACHMENT, attachment);
	}

	public List findByTotalscore(Object totalscore) {
		return findByProperty(TOTALSCORE, totalscore);
	}

	public List findByMailnow(Object mailnow) {
		return findByProperty(MAILNOW, mailnow);
	}

	public List findAll() {
		log.debug("finding all MdlForumPosts instances");
		try {
			String queryString = "from MdlForumPosts";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public MdlForumPosts merge(MdlForumPosts detachedInstance) {
		log.debug("merging MdlForumPosts instance");
		try {
			MdlForumPosts result = (MdlForumPosts) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(MdlForumPosts instance) {
		log.debug("attaching dirty MdlForumPosts instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(MdlForumPosts instance) {
		log.debug("attaching clean MdlForumPosts instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}